﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace HospitalSystem
{
    public partial class more : System.Web.UI.Page
    {
        DataClasses1DataContext db = new DataClasses1DataContext();
        private void MessageBox2(string msg, string urld)
        {
            Label lbl = new Label();
            lbl.Text = "<script language='javascript'>" + Environment.NewLine + "window.alert('" + msg + "');" + "window.location.href='" + urld + "';" + "</script>";
            Page.Controls.Add(lbl);
        }
        protected void Page_Load(object sender, EventArgs e)
        {

            if (Session["CaseNo"] == null)
            {
                MessageBox2("กรุณากดค้นหา และเลือกเด็กที่จะวางแผน", "checkcid3.aspx");
            }

            else
            {
                Label1.Text = "กิจกรรมอื่นๆ";

            }
        }

        protected void SubmitButton_Click(object sender, EventArgs e)
        {
            if (TextBox1_1.Text != String.Empty)
            {
                InsertData(TextBox1_1.Text);
            }
            if (TextBox2_1.Text != String.Empty)
            {
                InsertData(TextBox2_1.Text);
            }
            if (TextBox3_1.Text != String.Empty)
            {
                InsertData(TextBox3_1.Text);
            }
            if (TextBox4_1.Text != String.Empty)
            {
                InsertData(TextBox4_1.Text);
            }
            if (TextBox5_1.Text != String.Empty)
            {
                InsertData(TextBox5_1.Text);
            }
            if (TextBox6_1.Text != String.Empty)
            {
                InsertData(TextBox6_1.Text);
            }
            if (TextBox7_1.Text != String.Empty)
            {
                InsertData(TextBox7_1.Text);
            }
            if (TextBox8_1.Text != String.Empty)
            {
                InsertData(TextBox8_1.Text);
            }
            if (TextBox9_1.Text != String.Empty)
            {
                InsertData(TextBox9_1.Text);
            }
            if (TextBox10_1.Text != String.Empty)
            {
                InsertData(TextBox10_1.Text);
            }
        }

        private void InsertData(string msg)
        {
            //String Format
            Activity act = new Activity();
            ServiceProvisionDetail SPD = new ServiceProvisionDetail();
            var query = from a in db.Services
                        join b in db.Activities on a.SVCCode equals b.SVCCode
                        where a.SVCDEC.Equals(Label1.Text)
                        orderby b.ACTCode
                        select b.ACTCode;
            if (query.FirstOrDefault() == null)
            {
                var query2 = from a in db.Services
                             where a.SVCDEC.Equals(Label1.Text)
                             select a.SVCCode;
                foreach (var q2 in query2)
                {
                    act.SVCCode = q2;
                }
                act.ACTDesc = msg;
                act.ACTCode = "1";
                db.Activities.InsertOnSubmit(act);
                db.SubmitChanges();
            }
            else
            {
                
                //string formatString = "";
                //int twotoFivecharSVCCode = 0;
                //foreach (var q in query)
                //{
                //    formatString = q;
                //}
                //twotoFivecharSVCCode = (Convert.ToInt32(formatString[3]+formatString[4]))+1;
            }
            
        }
    }
}